#include "rootmodify.h"
#include "ui_rootmodify.h"
//#include "student.h"
//#include "regist.h"

#include <QMessageBox>  //提示窗功能
#include <QDebug>
#include <QPushButton>
#include <QAbstractButton>
#include <QCheckBox>
#include <QIODevice>
#include <QTextStream>
#include <QCursor>

rootModify::rootModify(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::rootModify)
{
    ui->setupUi(this);
    setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
    QSqlQuery query;
    query.exec("SELECT *FROM academy");
    while(query.next()) //获得学院下拉菜单
        this->ui->modi_academy->addItem(query.value("Academy").toString());
}

rootModify::~rootModify()
{
    delete ui;
}

void rootModify::on_modify_ok_clicked()
{
    //用户按下确定时读取表单的内容
    QString name = this->ui->modi_name->text();
    QString id = this->ui->modi_id->text();
    QString sex = this->ui->modi_sex_group->checkedButton()->text();
    QString tel = this->ui->modi_tel->text();
    QString grade = this->ui->modi_grade->currentText();
    QString classnum = this->ui->modi_class->currentText();
    QString academy = this->ui->modi_academy->currentText();
    QString major = this->ui->modi_major->currentText();
    QString politic = this->ui->modi_politics_group->checkedButton()->text();
    QString content = "姓名： " + name + "\n" + "学号： " + id + "\n" + "性别： " + sex + "\n" + "电话： " + tel + "\n" + "年级： "
            + grade + "\n" + "班级： " + classnum + "\n" + "学院： " + academy + "\n" + "专业： " + major + "\n" + "政治面貌： " + politic;
    QString pwd = "";
    if(this->ui->radio_pwd->isChecked()){
        pwd = this->ui->modi_pwd->text();
        content = content + "\n" + "密码： " + pwd;
    }

    QSqlQuery search;
    //返回学院、专业、政治面貌代码
    setCursor(Qt::BusyCursor);
    search.exec(QString("SELECT *FROM policy WHERE Policy = '%1'").arg(politic));
    search.next();
    QString Type = search.value("Type").toString();

    search.exec(QString("SELECT *FROM major WHERE Major= '%1'").arg(major));
    search.next();
    QString MID = search.value("MID").toString();
    QString AID = search.value("AID").toString();

    //姓名检测（1~5个汉字）
    bool name_error = !name.contains(QRegExp("^[\u4E00-\u9FA5]{1,5}$")); //全中文为false
    if(name.length() < 1)
        name_error = true;
    //qDebug() << "name" << name_error;

    //tel检测(11位纯数字)
    bool tel_error = tel.contains(QRegExp("[\\x20-\\x2f | \\x3a-\\x7e]+"));  //含非数字为true
    if(tel.length() != 11)
        tel_error = true;
    //qDebug() << "tel" << tel_error;

    //学号检测(首位大写字母+9位纯数字)
    bool id_error;
    if(id.length() != 10)
        id_error = true;
    else{
        id_error = id.right(9).contains(QRegExp("[\\x20-\\x2f | \\x3a-\\x7e]+"));  //后九位
        if(id[0] < 65 || id[0] > 90)    //首字符不为大写字母
            id_error = true;
    }
    //qDebug() << "UID" << id_error;


    //密码检测(6~10为不含空格的字符串)
    bool pwd_error = pwd.contains(QRegExp("[\\x20]+"));  //含空格为true
    if(pwd.length() < 6)
        pwd_error = true;
    //qDebug() << "pwd" << pwd_error;

    if(name_error){
        setCursor(Qt::ArrowCursor);
        QMessageBox::critical(this, "ERROR", "姓名填写错误（1~5个汉字）", "确定");
    }
    else if(tel_error){
        setCursor(Qt::ArrowCursor);
        QMessageBox::critical(this, "ERROR", "电话填写错误（11位纯数字）", "确定");
    }
    else if(id_error){
        setCursor(Qt::ArrowCursor);
        QMessageBox::critical(this, "ERROR", "学号格式错误（大写字母+9位纯数字）", "确定");
    }
    else if(this->ui->radio_pwd->isChecked() && pwd_error){
        setCursor(Qt::ArrowCursor);
        QMessageBox::critical(this, "ERROR", "密码填写错误（6~10位非空格字符）", "确定");
    }
    else{
        setCursor(Qt::ArrowCursor);
        int ret = QMessageBox::warning(this, "请确认信息", content, "确认", "取消");
        if(!ret){
            setCursor(Qt::BusyCursor);
            search.exec(QString("SELECT *FROM students WHERE UID = '%1'").arg(id));
            if(search.next() && search.value("UID").toString() != UID){
                setCursor(Qt::ArrowCursor);
                QMessageBox::critical(this, "ERROR", "学号重复", "确定");
                //return;
                this->showui();
            }
            else{
                QString update_user = QString("UPDATE users SET Account = '%1', "
                                              "WHERE Account = '%2'").arg(id).arg(UID);
                QString update_stu = QString("UPDATE students SET Name = '%1', UID = '%2', Sex = '%3', Politics = %4, "
                                             "Academy = %5, Major = %6, Grade = %7, Class = %8, Phone = '%9' "
                                             "WHERE UID = '%10'").arg(name)
                                             .arg(id).arg(sex).arg(Type).arg(AID).arg(MID).arg(grade).arg(classnum).arg(tel).arg(UID);
                QSqlQuery update;
                if(this->ui->radio_pwd->isChecked()){
                    QString update_pwd = QString("UPDATE users SET Password = '%1' WHERE Account = '%2'").arg(pwd).arg(UID);
                    //qDebug() << update_pwd;
                    update.exec(update_pwd);
                }
                if(update.exec(update_stu) == false)
                {
                    setCursor(Qt::ArrowCursor);
                    QMessageBox::critical(this, "ERROR", "所填信息不合法", "确定");
                    this->showui();
                }
                else{
                    update.exec(update_user);
                    setCursor(Qt::ArrowCursor);
                    QMessageBox::information(this, "SUCCESS", "修改成功", "确定");
                }
            }
        }
    }
}

void rootModify::on_modify_quit_clicked()
{
    menu->displayall();
    this->close();
}

void rootModify::showui()
{
    //将之前的用户数据填写到表单中
    QString sqlstr = RootMenu::selectstr(2, UID);
    QSqlQuery query;
    setCursor(Qt::BusyCursor);
    query.exec(sqlstr);
    query.next();
    this->ui->modi_name->setText(query.value("Name").toString());
    this->ui->modi_id->setText(query.value("UID").toString());
    this->ui->modi_grade->setCurrentText(query.value("Grade").toString());
    this->ui->modi_class->setCurrentText(query.value("Class").toString());
    this->ui->modi_tel->setText(query.value("Phone").toString());
    this->ui->modi_academy->setCurrentText(query.value("Academy").toString());
    this->ui->modi_major->setCurrentText(query.value("Major").toString());

    if(query.value("Sex").toString() == "女"){
        this->ui->modi_female->setChecked(true);
    }
    else{
        this->ui->modi_male->setChecked(true);
    }

    QString politics = query.value("Policy").toString();
    if(politics == "共产党员")
        this->ui->modi_p1->setChecked(true);
    else if(politics == "预备党员")
        this->ui->modi_p2->setChecked(true);
    else if(politics == "共青团员")
        this->ui->modi_p3->setChecked(true);
    else if(politics == "群众")
        this->ui->modi_p4->setChecked(true);

    this->ui->modi_pwd->clear();
    this->ui->modi_pwd->setReadOnly(true);
    this->ui->radio_pwd->setChecked(false);

    setCursor(Qt::ArrowCursor);
}

void rootModify::on_modi_academy_currentTextChanged(const QString &arg1)
{
    setCursor(Qt::BusyCursor);
    qDebug() << arg1;
    QSqlQuery query;
    query.exec(QString("SELECT *FROM academy WHERE Academy = '%1'").arg(arg1));
    query.next();
    QString AID = query.value("AID").toString();

    query.exec(QString("SELECT *FROM major WHERE AID = %1").arg(AID));
    this->ui->modi_major->clear();
    //this->ui->label_7->setVisible(true);
    //this->ui->addStu_major->setVisible(true);
    while(query.next()) //获得专业下拉菜单
        this->ui->modi_major->addItem(query.value("Major").toString());
    setCursor(Qt::ArrowCursor);
}

void rootModify::on_radio_pwd_clicked()
{
    if(this->ui->radio_pwd->isChecked()){
        this->ui->modi_pwd->setReadOnly(false);
    }
    else{
        this->ui->modi_pwd->clear();
        this->ui->modi_pwd->setReadOnly(true);
    }
}
